//单字母不补零
//多字母补零
//HH:24进制
//hh:12进制
//MM:月份
//mm:分钟
//am:am/pm
//AM:上午/下午
//ww:英文周
//WW:中文周
export class Time {
    data: Date;
    #weak: string[];
    #weake: string[];
    [key: string]: any
    constructor(time: string | number = Date.now()) {
        this.data = new Date(time)
        this.#weak = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
        this.#weake = ['Sun.', 'Sat.', 'Fri.', 'Thur.', 'Wed.', 'Tue.', 'Mon.']
    }
    getTime(formatter: string) {
        formatter.split(/[/|\-|:| |年|月|日|时|分|秒]/).forEach(item => {
            if (item.length > 4) return
            try {
                formatter = formatter.replace(item, this['#' + item])
            } catch {
                throw new Error("不存在‘" + item + "’处理方法")
            }
        })
        return formatter
    }
    countDown(formatter: string) {
        const leftTime = this.data.getTime() - Date.now()
        const D = Math.floor(leftTime / 1000 / 60 / 60 / 24);
        const H = Math.floor(leftTime / 1000 / 60 / 60 % 24);
        const m = Math.floor(leftTime / 1000 / 60 % 60);
        const s = Math.floor(leftTime / 1000 % 60);
        const completion: { [key: string]: string } = {
            DD: this.completion(D, 2),
            HH: this.completion(H, 2),
            mm: this.completion(m, 2),
            ss: this.completion(s, 2)
        }
        formatter.split(/[/|-|:| |日|时|分|秒]/).forEach(item => {
            if (item.length > 2) return
            try {
                if (Object.prototype.hasOwnProperty.call(completion, item)) {
                    formatter = formatter.replace(item, completion[item])
                }
            } catch {
                throw new Error("不存在‘" + item + "’处理方法")
            }
        })
        return formatter
    }
    get #Y() {
        return this.data.getFullYear()
    }
    get #M() {
        return this.data.getMonth() + 1
    }
    get #D() {
        return this.data.getDay()
    }
    get #H() {
        return this.data.getHours()
    }
    get #h() {
        const hours = this.data.getHours()
        if (hours > 12) {
            return hours - 12
        } else {
            return hours
        }
    }
    get #m() {
        return this.data.getMinutes()
    }
    get #s() {
        return this.data.getSeconds()
    }
    get #YYYY() {
        return this.completion(this.data.getFullYear(), 4)
    }
    get #MM() {
        return this.completion(this.data.getMonth() + 1, 2)
    }
    get #DD() {
        return this.completion(this.data.getDate(), 2)
    }
    get #HH() {
        return this.completion(this.data.getHours(), 2)
    }
    get #hh() {
        const hours = this.data.getHours()
        if (hours > 12) {
            return this.completion(hours - 12, 2)
        } else {
            return this.completion(hours, 2)
        }
    }
    get #mm() {
        return this.completion(this.data.getMinutes(), 2)
    }
    get #ss() {
        return this.completion(this.data.getSeconds(), 2)
    }
    get #AM() {
        const h = 12 - this.data.getHours()
        return h > 0 ? '上午' : '下午'
    }
    get #am() {
        const h = 12 - this.data.getHours()
        return h > 0 ? 'am' : 'pm'
    }
    get #WW() {
        return this.#weak[this.data.getDay()]
    }
    get #ww() {
        return this.#weake[this.data.getDay()]
    }
    plusYear(count: number = 1) {
        this.data = new Date(this.data.getTime() + 31536000000 * count)
    }
    plusMonth(count: number = 1) {
        this.data = new Date(this.data.getTime() + 2592000000 * count)
    }
    plusDay(count: number = 1) {
        this.data = new Date(this.data.getTime() + 86400000 * count)
    }
    plusHours(count: number = 1) {
        this.data = new Date(this.data.getTime() + 3600000 * count)
    }
    plusMinute(count: number = 1) {
        this.data = new Date(this.data.getTime() + 60000 * count)
    }
    plusSecond(count: number = 1) {
        this.data = new Date(this.data.getTime() + 1000 * count)
    }
    completion(target: number | string, count: number): string {
        target = target.toString()
        let length = target.length
        while (length < count) {
            target = '0' + target
            length = target.length
        }
        return target
    }
}